﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
namespace SQT.tracnghiem.Admin
{
    public partial class Staff_Personal : System.Web.UI.Page
    {
        public int? id, old, province = null, city = null, day, month, year,last_year=0,last_id;
        public int get_year = DateTime.Now.Year - 5, hour = 0, minute = 0, week = 0;
        public string banner = "", avatar = "", fullname = "", email = "", sex = "Nam", address = "Chưa cập nhật", phone = "",accept="";
        public bool captain = false, check_sex = true;
        public DateTime? monday,week_start;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                //////////////////////// Thông tin cá nhân //////////////////////////////////
                id = int.Parse(RouteData.Values["id"].ToString());
                var find_staff = new TblManagerController().FetchAll().Where(x => x.ManagerId == id).ToList();
                if (find_staff.Count == 0 && Session["admin_id"] == null)
                {
                    Response.Redirect("~/quan-ly/trang-tong-quan");
                }
                else
                {
                    banner = find_staff[0].Wallpaper;
                    avatar = find_staff[0].Avatar;
                    fullname = find_staff[0].FullName;
                    email = find_staff[0].Email;
                    phone = find_staff[0].Phone;
                    if (find_staff[0].Sex == false)
                    {
                        sex = "Nữ";
                        check_sex = false;
                    }
                    old = DateTime.Now.Year - DateTime.Parse(find_staff[0].Birthday.ToString()).Year;
                    if (find_staff[0].Address != null)
                    {
                        var get_city = new TblCityController().FetchAll().Where(x => x.Id == find_staff[0].Address).Single();
                        var get_province = new TblProvinceController().FetchAll().Where(x => x.Id == get_city.ProvinceId).Single();
                        address = get_city.CityName + " - " + get_province.ProvinceName;
                        city = int.Parse(find_staff[0].Address.ToString());
                        province = int.Parse(get_province.Id.ToString());
                    }
                    if (find_staff[0].Captain == true)
                        captain = true;
                    day = DateTime.Parse(find_staff[0].Birthday.ToString()).Day;
                    month = DateTime.Parse(find_staff[0].Birthday.ToString()).Month;
                    year = DateTime.Parse(find_staff[0].Birthday.ToString()).Year;

                    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                    //////////////////////////////// Thống kê giờ làm ////////////////////////////////////////

                    foreach (TblCheckManager chk in new TblCheckManagerController().FetchAll().Where(x => x.ManagerId == id && DateTime.Parse(x.CheckIn.ToString()).Month == DateTime.Now.Month && DateTime.Parse(x.CheckIn.ToString()).Year == DateTime.Now.Year && x.Accept == true))
                    {
                        if (chk.CheckOut != null)
                        {
                            if (DateTime.Parse(chk.CheckOut.ToString()).Hour >= DateTime.Parse(chk.CheckIn.ToString()).Hour)
                            {
                                hour += DateTime.Parse(chk.CheckOut.ToString()).Hour - DateTime.Parse(chk.CheckIn.ToString()).Hour;
                            }
                            else
                            {
                                hour += 24 - DateTime.Parse(chk.CheckIn.ToString()).Hour + DateTime.Parse(chk.CheckOut.ToString()).Hour;
                            }

                            if (DateTime.Parse(chk.CheckIn.ToString()).Minute <= DateTime.Parse(chk.CheckOut.ToString()).Minute)
                            {
                                minute += DateTime.Parse(chk.CheckOut.ToString()).Minute - DateTime.Parse(chk.CheckIn.ToString()).Minute;
                            }
                            else
                            {
                                minute += 60 - DateTime.Parse(chk.CheckIn.ToString()).Minute + DateTime.Parse(chk.CheckOut.ToString()).Minute;
                                hour--;
                            }
                        }
                    }
                    if (minute >= 60)
                    {
                        int temp = minute / 60;
                        hour += temp;
                        minute = minute - (temp * 60);
                    }

                    ///////////////////////////////////////////// Lịch làm việc //////////////////////////////////////////////
                    /////////////////////////////////////////////////////////////////////////////////////////////////////////////

                    var get_last_year = new TblWeekController().FetchAll().Last();
                    last_year = get_last_year.Year;
                    last_id = get_last_year.Id;
                    week_start = get_last_year.Monday;
                    week = WeekOfYear(DateTime.Now);
                    week--;
                    monday = MondayOfWeek(week,DateTime.Now.Year);
                }
            }
            catch
            {
                Response.Redirect("~/quan-ly/trang-ca-nhan");
            }
        }

        private int WeekOfYear(DateTime date)
        {
            // use the current culture
            System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
            // use current culture's calendar
            System.Globalization.Calendar cal = ci.Calendar;
            // get the calendar week rule (i.e. what determines the first week in the year)
            System.Globalization.CalendarWeekRule cwr = ci.DateTimeFormat.CalendarWeekRule;
            // get the first day of week for the current culture
            DayOfWeek fdow = ci.DateTimeFormat.FirstDayOfWeek;
            // return the week
            return cal.GetWeekOfYear(date, cwr, fdow);
        }

        private static DateTime MondayOfWeek(int week,int year)
        {
            string year1 = year.ToString();
            year1 += "-01-01";
            DateTime d1 = DateTime.Parse(year1);

            d1 = d1.AddDays(7 * week);
            if (d1.DayOfWeek == DayOfWeek.Sunday)
                return d1.AddDays(-6);

            int monday = d1.DayOfWeek - DayOfWeek.Monday;
            return d1.AddDays(-monday);
        }

        public bool check_accept_menu(string role)
        {
            if (int.Parse(Session["admin_id"].ToString()) != this.id)
                return false;
            string[] split = role.Split(',');
            for(int i=0;i<split.Length-1;i++)
            {
                if (int.Parse(Session["admin_id"].ToString()) == int.Parse(split[i]))
                    return true;
            }
            return false;
        }
        public bool check_role_menu()
        {
            if (int.Parse(Session["admin_id"].ToString()) != this.id)
                return false;
            var get_menu = new TblAdminMenuController().FetchAll().Where(x => x.AmIsDelete == false && !String.IsNullOrEmpty(x.AmCaptain));
            foreach(TblAdminMenu menu in get_menu)
            {
                string[] split = menu.AmCaptain.Split(',');
                for (int i = 0; i < split.Length - 1; i++)
                {
                    if (int.Parse(Session["admin_id"].ToString()) == int.Parse(split[i]))
                        return true;
                }
            }
            return false;
        }
    }
}